// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const { result } = require('product-api-utils');
const uniID = require('uni-id-common');

module.exports = {
	_before: function () {
		this.startTime = Date.now();

		// 创建uni-id实例
		const clientInfo = this.getClientInfo();
		this.uniIDIns = uniID.createInstance({
			clientInfo
		});

		// 获取body参数
		this.params = {};
		const httpInfo = this.getHttpInfo();
		if (httpInfo) {
			const body = httpInfo.body;
			if (!body) throw result(400, 'required', 'body不能为空！');
			this.params = JSON.parse(body);
		}
	},

	// 校验token，获取用户的uid、token、token的过期时间、角色、权限
	checkToken() {
		const { token } = this.params;
		if (!token) throw result(400, 'required', 'token不能为空！');
		return this.uniIDIns.checkToken(token);
	},

	_after: function (error, result) {
		if (error) {
			throw error;
		}
		result.timeCost = Date.now() - this.startTime;
		result.author = '接口提供者：明致成';
		return result;
	}
};
